<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="120px" v-show="showSearch">
            <el-form-item label="省" prop="provinceId">
                <el-select v-model="queryParams.provinceId" placeholder="请选择省" @change="chooseProvince" :disabled="!isAdmin" clearable>
                    <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="市" prop="cityId">
                <el-select v-model="queryParams.cityId" placeholder="请选择市" @change="chooseCity" clearable>
                    <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="区/县" prop="prefectureId">
                <el-select v-model="queryParams.prefectureId" placeholder="请选择区/县" clearable>
                    <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="报告分类" prop="srType">
                <el-select v-model="queryParams.srType" placeholder="请选择报告类型" clearable>
                    <el-option v-for="item in optionType" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="活动名称" prop="iaName">
                <el-input v-model="queryParams.iaName" placeholder="请输入活动名称" clearable />
            </el-form-item>
            <el-form-item label="报告名称" prop="srName">
                <el-input v-model="queryParams.srName" placeholder="请输入报告名称" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">搜索</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" @click="handleDownloads" :disabled="multiple">
                    <svg-icon class-name="download-icon" icon-class="download" /><span
                        style="margin-left:10px">批量下载</span>
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="报告名称" align="center" prop="srName" width="250" show-overflow-tooltip>
                <template #default="scope">
                    <span style="color: #409eff;">{{scope.row.srName}}</span>
                </template>
            </el-table-column>
            <el-table-column label="报告分类" align="center" prop="srType" />
            <el-table-column label="活动名称" align="center" prop="iaName" width="250" show-overflow-tooltip/>
            <el-table-column label="省" align="center" prop="iaProvince" />
            <el-table-column label="市" align="center" prop="iaCity" />
            <el-table-column label="区（县）" align="center" prop="iaCounties" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" @click="handleDownload(scope.row)">下载</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />
    </div>
</template>
  
<script setup name="reportQuery">
import { getProvince, getChildArea } from "@/api/system/division";
import { getAllList,getPermission } from "@/api/resultsReportImg/reportQuery.js"
const { proxy } = getCurrentInstance();
const dataList = ref([]);
const loading = ref(false);
const total = ref(0);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const provinceData = ref([]);//省
const cityData = ref([]);//市
const areaData = ref([]);//区、县
const isAdmin = ref(true);
const optionType = [{ label: '地震灾害风险评估报告', value: '地震灾害风险评估报告' }, { label: '辅助对策报告', value: '辅助对策报告' }]
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        provinceId: '',
        cityId: '',
        prefectureId: '',
        srType: '',
        iaName: '',
        srName: '',
    },
})
const { queryParams } = toRefs(data)
/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}
/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    loading.value = true;
    getAllList(queryParams.value).then(response => {
        dataList.value = response.data.actReports?response.data.actReports:[];
        total.value = response.data.total;
        loading.value = false;
    });
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.attachId)
    console.log(ids.value);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}

/** 批量下载操作 */
function handleDownloads() {
    proxy.download("/attachment/downForZip/"+ ids.value.join(","), { }, '报告.zip');

};

// 单个下载文件
function handleDownload(row) {
	var query = {
		attachId:row.attachId,
		name:row.srName
	}
	location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId="+ encodeURIComponent(query.attachId);
}

/** 获取省列表 */
function getProvinceData() {
    getProvince().then(response => {
        provinceData.value = response.data;
        getPermissionData();
    });
}
function chooseProvince(value) {
    queryParams.value.cityId = null;
    queryParams.value.prefectureId = null;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value).then(response => {
        cityData.value = response.data;
    });
}

function chooseCity(value) {
    queryParams.value.prefectureId = null;
    areaData.value = [];
    getChildArea(value).then(response => {
        areaData.value = response.data;
    });
}

function getPermissionData(){
    getPermission().then(res=>{
        isAdmin.value = res.data.isAdmin;
        // 如果是国家级人员
        if(isAdmin.value){
            queryParams.value.provinceId = ''
        }else{
            queryParams.value.provinceId = res.data.provinceId;
        }
        getList();
    });
}

getProvinceData();
// getList();
</script>
  
<style lang="scss" scoped>

</style>